import java.util.HashMap;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-07-24
 * Time: 13:35
 */
class Student {
    String name;

    public Student(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
public class Test {
    public static void main(String[] args) {
        HashMap<Student,String> map = new HashMap<>();
        Student s1 = new Student("xiao");
        Student s2 = new Student("xiao");
        map.put(s1,"123");
        System.out.println(s1.equals(s2));

        //重写了hashCode方法，故s1和s2哈希值相同，能够找到s1在数组中的下标
        //重写了equals方法 所以能通过s2找到s1
        System.out.println(map.get(s2));
        System.out.println(map);
    }
}
